import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { priority } from "utils/constants/definetypes";
import { role_crossLevel } from "utils/constants/moveConstant";
import { SpawnLayoutConfig } from "GameLoop/WorldState/EntityManager/Entitys/room/spawn/spawnlayout";
import { EActionCode } from "framework/FSM/Action/ActionCode";
import { hold_place } from "GameLoop/WorldState/mount/move/action.virtual";
import { Math2D } from "algorithm/Math2d";
import { StationNetwork } from "GameLoop/Path/StationNetwork";


/**
 * 升级Action
 */
/*@profile*/
export class ActionFill  extends ActionBaseOfCreep<IActionCMD<"fill">>
{
    protected _execute(action: IActionCMD<"fill">): EActionCode 
    {
        const cpu_1 = Game.cpu.getUsed();
        const creep = action.arg.entity;
       
        const spawnentity = creep.getEntity("SpawnEntity",creep.memory.OwnerRoom);
        const workpos =  action.arg.workpos;
        //creep.log('info','填充对象',creep.pos,JSON.stringify(workpos),Math2D.distanceMhd( creep.pos,workpos));
        const p_abs = spawnentity.getAbsulotePosition(workpos);
        if(Math2D.distanceMhd( creep.pos,p_abs)!=0)
        {
            return EActionCode.FAILED;
        }
        const cpuput={text:'',type:"warn"};
        const cpu_2 = Game.cpu.getUsed();
        
        this.concatLog(cpuput ,'cpu_2',cpu_2-cpu_1);
        if(creep.room.controller!.level>=8 && creep.ticksToLive && creep.ticksToLive<900)
        {
            const spawn = spawnentity.getSpawn(workpos);
            if(spawn)
            {
                if(!spawn.spawning)
                {
                    spawn.renewCreep(creep);
                }
            }
            else 
            {
                spawnentity.log("error",JSON.stringify(workpos),'找不到对应的spawn');
            }
        }
        const cpu_3 = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_3',cpu_3-cpu_2);
        hold_place(creep);
        const cpu_4 = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_4',cpu_4-cpu_3);
        StationNetwork.getInstance.PathFinder.RegisterBlockCreep(creep)
        const cpu_5a = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_5a',cpu_5a-cpu_4);
        const transferobj = spawnentity.getTransferObject(workpos);
        const cpu_5b = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_5b',cpu_5b-cpu_5a);
        //creep.log('info','填充对象',transferobj.map(e=>e.pos),'注册阻挡',Object.keys(StationNetwork.getInstance.PathFinder._blockCreeps[creep.room.name]));
        const cpu_5 = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_5',cpu_5-cpu_4);
        if(transferobj.length>0)
        {
            const cpu_6_a = Game.cpu.getUsed();
            const withobject = spawnentity.getStoreObject(workpos);
            const cpu_6_b = Game.cpu.getUsed(); 
            this.concatLog(cpuput ,'cpu_6_b-a',cpu_6_b-cpu_6_a);
            if(withobject.length>0)
            {
                // 填充能量
                creep.withdraw(withobject[0],RESOURCE_ENERGY);
                creep.transfer(transferobj[0],RESOURCE_ENERGY);
                
            }
            else
            {
              
                const resource = spawnentity.getPickUpResource();
                const cpu_6_c = Game.cpu.getUsed();
                this.concatLog(cpuput ,'cpu_6_c-b',cpu_6_c-cpu_6_b);
                if(resource)
                {
                    creep.pickup(resource);
                    creep.transfer(transferobj[0],RESOURCE_ENERGY);
                    
                }
                else
                {
                    creep.transfer(transferobj[0],RESOURCE_ENERGY);
                }
            }
            const cpu_6_d = Game.cpu.getUsed();
            this.concatLog(cpuput ,'cpu_6_d-b',cpu_6_d-cpu_6_b);
        }
        else {
            const cpu_6_e = Game.cpu.getUsed();
            const resource = spawnentity.getPickUpResource();
            const cpu_6_f = Game.cpu.getUsed();
            this.concatLog(cpuput ,'cpu_6_f-e',cpu_6_f-cpu_6_e);
            const withobject = spawnentity.getStoreObject(workpos,false,(e:AnyStoreStructure)=>e.store.getFreeCapacity(RESOURCE_ENERGY)>0);
            const cpu_6_g = Game.cpu.getUsed();
            this.concatLog(cpuput ,'cpu_6_g-f',cpu_6_g-cpu_6_f);
            if(resource && withobject.length>0)
            {
                creep.pickup(resource);
                creep.transfer(withobject[0],RESOURCE_ENERGY);
                return EActionCode.RUNNING;
            }
           
            if(spawnentity.Link && spawnentity.Link.store.getUsedCapacity(RESOURCE_ENERGY)>100)
            {
                const cpu_6_h = Game.cpu.getUsed();
                this.concatLog(cpuput ,'cpu_6_h-g',cpu_6_h-cpu_6_g);
                if(withobject.length>0)
                {
                    creep.withdraw(spawnentity.Link,RESOURCE_ENERGY);
                    creep.transfer(withobject[0],RESOURCE_ENERGY);
                     
                }
            }
        }
        const cpu_6 = Game.cpu.getUsed();
        this.concatLog(cpuput ,'cpu_6',cpu_6-cpu_5);
        creep.memory.Debug['log']=cpuput.text;
        return EActionCode.RUNNING;
    }
}